#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int timestamp2bcd(long timestamp, unsigned char bcd_buf[]) {
	//时区不同.手动加八个小时
	timestamp = timestamp + 28800; //北京时间

	struct tm *time;
	time = gmtime(&timestamp);

	char buffer[20];
	strftime(buffer, 20, "%y%m%d%H%M%S", time);

	bcd_buf[0] = (unsigned char)((buffer[0] - '0') << 4) + (buffer[1] - '0');
	bcd_buf[1] = (unsigned char)((buffer[2] - '0') << 4) + (buffer[3] - '0');
	bcd_buf[2] = (unsigned char)((buffer[4] - '0') << 4) + (buffer[5] - '0');
	bcd_buf[3] = (unsigned char)((buffer[6] - '0') << 4) + (buffer[7] - '0');
	bcd_buf[4] = (unsigned char)((buffer[8] - '0') << 4) + (buffer[9] - '0');
	bcd_buf[5] = (unsigned char)((buffer[10] - '0') << 4) + (buffer[11] - '0');

	return 0;
}

int main () {
	time_t tmp;

	long  timestamp = time(&tmp);

	printf("timestamp :%ld\n", timestamp);

	unsigned char BCD_Time[6] = {0};

	timestamp2bcd(timestamp, BCD_Time);

	int i;
	printf("BCD_Time  :");
	for (i = 0; i < 6; i++) {
		printf("%02x", BCD_Time[i]);
	}
	printf("\n");
	
	return (0);
}
